package com.shop.product.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.shop.product.service.ProductOptService;
import com.shop.product.vo.ProductVO;

/**
 * @author geryon
 *
 */
@Controller
@RequestMapping("/PRODUCT/OPT")
public class ProductOptController {

	private final static int LIST_ROW_CNT = 20; // 게시물 리스트 갯수
	
	@Autowired
	private ProductOptService service;

	
	/**
	 * 옵션리스트
	 * 리스트 화면을 호출한다
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value="")
	public String requestScreen(Model model) {
		return "product/option_list";
	}

	/**
	 * 옵션 마스터 리스트
	 * @param model
	 * @param member
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value="/")
	public Model listOpt(Model model,
							@RequestParam ("page_no") int page_no) {
		// 총 이미지 건수
		int tot_cnt = 0;
		// 총 페이지 수
		int tot_page_cnt = 0;
		
		tot_cnt = service.totalCnt();

		tot_page_cnt = tot_cnt / LIST_ROW_CNT + (((tot_cnt % LIST_ROW_CNT) == 0) ? 0 : 1);
				
		List<ProductVO.ProductOptMaster> list = service.listOptMaster(page_no, LIST_ROW_CNT);		
		model.addAttribute("MODEL", list);
		model.addAttribute("TOTAL_CNT", tot_cnt);
		model.addAttribute("TOTAL_PAGE", tot_page_cnt);		
		return model;
	}
	
	/**
	 * 옵션 등록
	 * @param model
	 * @param member
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value="/")
	public Model addOpt(Model model,
							@RequestBody ProductVO.ProductOptMaster optItem) {
	
		int SEQ = 0;
		SEQ = service.addOpt(optItem);
		model.addAttribute("MODEL", SEQ);
		return model;		
	}

	/**
	 * 옵션 상세조회
	 * @param model
	 * @param member
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value="/{seq}")
	public Model getOpt(Model model,
							@PathVariable ("seq") int seq) {
		System.out.println("getGET Call Method");
		ProductVO.ProductOptMaster vo = (ProductVO.ProductOptMaster)service.getOpt(seq);
		model.addAttribute("MODEL", vo);
		return model;
	}

	/**
	 * 옵션 수정
	 * @param model
	 * @param member
	 * @return
	 */
	@RequestMapping(method = RequestMethod.PUT, value="/")
	public Model updateOpt(Model model,
							@RequestBody ProductVO.ProductOptMaster optItem) {
		service.updateOpt(optItem);
		model.addAttribute("MODEL", optItem);
		return model;

	}

	/**
	 * 옵션 삭제
	 * @param model
	 * @param member
	 * @return
	 */
	@RequestMapping(method = RequestMethod.DELETE, value="/{seq}")
	public Model deleteOpt(Model model,
							@PathVariable ("seq") String opt_master_seq) {
		return model;
	}
}


